import requests
from util.get_time_stamp import get_time_stamp
import re
from util.get_header import get_headers_init_user_agent
headers = get_headers_init_user_agent()

session = requests.Session()



months = [f"{i:02d}" for i in range(1, 13)]

for month in months:
    # 历史上的今天
    url = f'https://baike.baidu.com/cms/home/eventsOnHistory/{month}.json?_={get_time_stamp()}'

    res = requests.get(url, headers=headers)

    new_cookie = {
        'BAIDUID': res.cookies['BAIDUID'],
        'ab_sr': '1.0.1_ZTk1M2E1MDM5ZDhkMzI0ZGI2N2I0ZWY1NjBhMGMyMTMzNDA0MTcxNDA0NWEwYTM4NjBhYzhjZTQ3YmU5YjZjYmE5YWYyYmJjNzY0ZGRkOTU3ZDRmMGMxZjk4MDNjMjQ2NTQ1N2U0YTc1MjQ5YzI1NTFmYjQ3NGNkZGQ2ZGEzYzA0MWFkM2NmOGRjMzkzOGFjZmI2ODc1Yzg2MzFlODUwNA==',

    }

    for k, v in res.json()[month].items():
        print(k)
        for item in v:
            title = re.sub(r'<a target="_blank" href=".*?">', '', item['title'])
            title = re.sub(r'</a>', '', title)
            title = re.sub(r'<a href=".*?">', '', title, flags=re.S)
            print(title, item['link'])

            detail_response = requests.get(item['link'],
                cookies=new_cookie,
                headers=headers,
            )
            print(detail_response.text)
            break
        break
    break






# from curl_cffi import requests
# res = requests.get("https://baike.baidu.com/item/%E7%8E%8B%E5%AE%88%E4%BB%81/503207")
# print(res.text)